#include "CGamePauseState.h"
#include "CMainMenuState.h"
#include "CGame.h"
#include "COptionMenuState.h"
#include "CGamePlayState.h"
#include "CXInput.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_DirectSound.h"
#include "SGD Wrappers/CSGD_WaveManager.h"
#include "SGD Wrappers/CSGD_FModManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "CExitState.h"

CGamePauseState::CGamePauseState()
{
	Type(SUB);
	m_bfItem.Init("Ludicrum_Reclamation_Alt.fnt");
	m_nBGImage = CSGD_TextureManager::GetInstance()->LoadTexture("resource/graphics/Pause_Back.png");
}

CGamePauseState::~CGamePauseState()
{

}

CGamePauseState* CGamePauseState::GetInstance()
{
	static CGamePauseState instance;
	return &instance;
}

void CGamePauseState::Enter()
{
	/////////////////////////////////////////////////
	//	Menu Items
	CMenuItem miPlay;
	miPlay.Init(&m_bfItem, 200, 0, "Resume", CGamePlayState::GetInstance(), 1.0f);
	miPlay.GoTo(300, 200);
	m_vMenuItems.push_back(miPlay);
	CMenuItem miOptions;
	miOptions.Init(&m_bfItem, 200, 0, "Options", COptionMenuState::GetInstance(), 1.0f);
	miOptions.GoTo(300, 300);
	m_vMenuItems.push_back(miOptions);
	CMenuItem miExit;
	miExit.Init(&m_bfItem, 200, 0, "Exit", CMainMenuState::GetInstance(), 1.0f);
	miExit.GoTo(300, 400);
	m_vMenuItems.push_back(miExit);
	//
	/////////////////////////////////////////////////
}

bool CGamePauseState::Input()
{
	if(CSGD_DirectInput::GetInstance()->KeyPressed(DIK_ESCAPE))
	{
		CGame::GetInstance()->PopState();
	}

	if(	CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_DOWN) || 
		CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RIGHT) ||
		((CXInput::GetInstance()->LStickY() <= -7850 ||
		CXInput::GetInstance()->LStickX() >= 7850) && CXInput::GetInstance()->IsLStickMoved() == false))
	{
		CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChoiceSound());
		CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChoiceSound(), CGame::GetInstance()->SFXVol());
		CXInput::GetInstance()->IsLStickMoved(true);
		vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();
		for(unsigned int i = 0; i < m_vMenuItems.size() - 1; i++)
			StuffIter++;
		float fTempX = StuffIter->TarPosX();
		float fTempY = StuffIter->TarPosY();

		while(StuffIter != m_vMenuItems.begin())
		{
			vector<CMenuItem>::iterator temp = StuffIter;

			StuffIter--;

			temp->GoTo(StuffIter->TarPosX(), StuffIter->TarPosY());

			if(StuffIter->Target() == m_vMenuItems[0].Target())
			{
				StuffIter->GoTo(fTempX, fTempY);
				break;
			}
		}
	}

	if(	CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_UP) || 
		CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_LEFT) ||
		((CXInput::GetInstance()->LStickY() >= 7850 ||
		CXInput::GetInstance()->LStickX() <= -7850) && CXInput::GetInstance()->IsLStickMoved() == false))
	{
		CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChoiceSound());
		CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChoiceSound(), CGame::GetInstance()->SFXVol());
		CXInput::GetInstance()->IsLStickMoved(true);
		vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();
		float fTempX = StuffIter->TarPosX();
		float fTempY = StuffIter->TarPosY();

		while(StuffIter != m_vMenuItems.end())
		{
			vector<CMenuItem>::iterator temp = StuffIter;

			StuffIter++;

			temp->GoTo(StuffIter->TarPosX(), StuffIter->TarPosY());

			if(StuffIter->Target() == m_vMenuItems[m_vMenuItems.size()-1].Target())
			{
				StuffIter->GoTo(fTempX, fTempY);
				break;
			}
		}
	}
	if((CXInput::GetInstance()->LStickY() > -7850 && CXInput::GetInstance()->LStickY() < 7850 &&
		CXInput::GetInstance()->LStickX() < 7850 &&	CXInput::GetInstance()->LStickX() > -7850)
		&& CXInput::GetInstance()->IsLStickMoved() == true)
	{
		CXInput::GetInstance()->IsLStickMoved(false);
	}

	if((CSGD_DirectInput::GetInstance()->KeyUp(DIK_LALT)) && ((CSGD_DirectInput::GetInstance()->KeyPressedEx(DIK_RETURN)) ||
		CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A && CXInput::GetInstance()->GetIsADown() == false))
	{
		CSGD_FModManager::GetInstance()->PlaySound(CGame::GetInstance()->ChosenSound());
		CSGD_FModManager::GetInstance()->SetVolume(CGame::GetInstance()->ChosenSound(), CGame::GetInstance()->SFXVol());
		CXInput::GetInstance()->SetIsADown(true);
		vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

		while(StuffIter != m_vMenuItems.end())
		{
			if(StuffIter->TarPosY() == 200)
			{
				if(StuffIter->Name() == "Resume")
				{
					CGame::GetInstance()->PopState();
					return true;
				}
				/*else if(StuffIter->Name() == "Exit")
				{
					CGame::GetInstance()->ChangeState(NULL);
					return false;
				}*/
				else if(StuffIter->Target()->Type() == SUB)
					CGame::GetInstance()->PushState(StuffIter->Target());
				else
				{
					CGame::GetInstance()->ChangeState(StuffIter->Target());
					return true;
				}
			}
			++StuffIter;
		}
	}
	else if(!(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A) && CXInput::GetInstance()->GetIsADown() == true)
		CXInput::GetInstance()->SetIsADown(false);
	return true;
}

void CGamePauseState::Update(float fDelta)
{
	vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

	while(StuffIter != m_vMenuItems.end())
	{
		StuffIter->Update(fDelta);
		++StuffIter;
	}
}

void CGamePauseState::Render()
{
	CSGD_TextureManager::GetInstance()->Draw(m_nBGImage, 0, 0);
	m_bfItem.DrawBitmapText("Pause Menu", 250, 100, 2.0f);

	vector<CMenuItem>::iterator StuffIter = m_vMenuItems.begin();

	while(StuffIter != m_vMenuItems.end())
	{
		if(StuffIter->TarPosY() == 200)
			StuffIter->Render(true);
		else
			StuffIter->Render();

		++StuffIter;
	}
}

void CGamePauseState::Exit()
{
	m_vMenuItems.clear();
}
